﻿using Hr.IDAL;
using Hr.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Linq.Expressions;
using System.Net.NetworkInformation;
using System.Runtime.Remoting.Messaging;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Hr.DAL
{
    public partial class CarDAL : BaseDAL<Car_List>, ICar_ListDAL
    {
        private DbContext db = DbContextFactory.Create();


        //核心
        /// <summary>
        /// 获取用车管理信息,跟据ID
        /// </summary>
        /// <param name="t"></param>
        /// <returns>null</returns>
        /// <Autor>唐斌</Autor>
        /// <Create>2020/08/07</Create>
        /// <Update>null</Update>
        public Car_List GetCarList(int id)
        {
            return db.Set<Car_List>().SingleOrDefault(n => n.pk_Car == id);
        }

        /// <summary>
        /// 根据id,获取车辆管理信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Car_Set GetSetinfo(int id)
        {
            return db.Set<Car_Set>().SingleOrDefault(n => n.pk_Set == id);
        }

        /// <summary>
        /// 根据ID获取审核信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetCheckInfo(int id)
        {

            return db.Set<Car_List>().FirstOrDefault(n => n.pk_Car == id);

        }

        /// <summary>
        /// 模糊查询，查询车辆信息
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public IEnumerable<object> GetCarSet(string Title)
        {
            if (Title == null)
            {
                Title = "";
            }
            return (from CarList in db.Set<Car_List>()
                    join set in db.Set<Car_Set>() on CarList.fk_Set equals set.pk_Set
                    join User in db.Set<User_List>() on CarList.fk_User equals User.pk_User
                    join Department in db.Set<User_Department>() on CarList.fk_Department equals Department.pk_Department
                    where CarList.Title.Contains(Title)
                    select new
                    {
                        CarList.pk_Car,
                        TypeName_CHS = db.Set<Sys_TypeID>().Where(n => n.TableName.Equals("Car_List") & n.ID == CarList.TypeID).FirstOrDefault().TypeName_CHS,
                        CarList.Title,
                        CarList.CreateDate,
                        CarList.IsCheck,
                        CarList.CheckDate,
                        User.FullName,
                        StatusName_CHS = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Car_List") & n.ID == CarList.StatusID).FirstOrDefault().StatusName_CHS,
                        Visual = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Car_List") & n.ID == CarList.StatusID).FirstOrDefault().Visual,
                        CarList.IsBack,
                        CarList.BackDate,
                        CarTitle = set.Title

                    }
                );
        }

        /// <summary>
        /// 获取用车类型集合
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Car_Set> GetCarListType()
        {
            //去用车管理里面查询是否有使用
            //先循环所有车辆
            //List<Car_Set> CarSet = new List<Car_Set>();
            //foreach (Car_Set c in db.Set<Car_Set>())
            //{

            //    //再去carlist审核表里面查找是否在使用时间，如果在使用时间就不加到集合里面去，如果使用归还时间早于到期时间，把车辆状态改为0 可使用
            //    //去审核表里面看看这辆车有没有审核通过的在使用的
            //    //Car_List CL=db.Set<Car_List>().Where(n=>n.fk_Set==c.pk_Set&n.StatusID==1).FirstOrDefault
            //}
            return db.Set<Car_Set>().Where(n => n.StatusID == 0);
        }

        /// <summary>
        /// 通过表名获取类型集合
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<Sys_TypeID> GetTypeList(string name)
        {
            return db.Set<Sys_TypeID>().Where(n => n.TableName.Equals(name)).ToList();
        }

        /// <summary>
        /// 获取车辆信息集合
        /// </summary>
        /// <returns></returns>
        public IEnumerable<object> GetCarSetList(string SelName)
        {
            if (SelName != null)
            {
                return (from set in db.Set<Car_Set>()
                        where set.Title.Contains(SelName) || set.TypeID == db.Set<Sys_TypeID>().Where(n => n.TableName.Equals("Car_Set") & n.TypeName_CHS.Contains(SelName)).FirstOrDefault().ID
                        select new
                        {
                            set.pk_Set,
                            set.Title,
                            set.BuyDate,
                            set.LastMaintenance,
                            set.Mileage,
                            set.StatusID,
                            StatusName_CHS = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Car_Set") & n.ID == set.StatusID).FirstOrDefault().StatusName_CHS,
                            TypeName_CHS = db.Set<Sys_TypeID>().Where(n => n.TableName.Equals("Car_Set") & n.ID == set.TypeID).FirstOrDefault().TypeName_CHS,
                            Visual = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Car_Set") & n.ID == set.StatusID).FirstOrDefault().Visual
                        }
                        );
            }
            else
            {
                return (from set in db.Set<Car_Set>()
                        select new
                        {
                            set.pk_Set,
                            set.Title,
                            set.BuyDate,
                            set.LastMaintenance,
                            set.Mileage,
                            set.StatusID,
                            StatusName_CHS = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Car_Set") & n.ID == set.StatusID).FirstOrDefault().StatusName_CHS,
                            TypeName_CHS = db.Set<Sys_TypeID>().Where(n => n.TableName.Equals("Car_Set") & n.ID == set.TypeID).FirstOrDefault().TypeName_CHS,
                            Visual = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Car_Set") & n.ID == set.StatusID).FirstOrDefault().Visual
                        }
               );
            }

        }

        /// <summary>
        /// 获取审批管理信息,带模糊查询
        /// </summary>
        /// <returns></returns>
        public IEnumerable<object> GetCheckList(string Title)
        {
            if (Title == null)
            {
                return (from CarList in db.Set<Car_List>()
                        join set in db.Set<Car_Set>() on CarList.fk_Set equals set.pk_Set
                        join User in db.Set<User_List>() on CarList.fk_User equals User.pk_User
                        join Department in db.Set<User_Department>() on CarList.fk_Department equals Department.pk_Department
                        select new
                        {
                            CarList.pk_Car,
                            CarList.Title,
                            CarList.CreateDate,
                            CarTitle = set.Title,
                            CarList.IsCheck,
                            CarList.CheckDate,
                            User.FullName,
                            CarList.IsBack,
                            CarList.BackDate,
                            Department.DepartmentName,
                            CarList.CreateUser,
                            CarList.StatusID,
                            CarList.CheckUser,

                            StatusName_CHS = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Car_List") & n.ID == CarList.StatusID).FirstOrDefault().StatusName_CHS,
                            Visual = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Car_List") & n.ID == CarList.StatusID).FirstOrDefault().Visual,
                            TypeName_CHS = db.Set<Sys_TypeID>().Where(n => n.TableName.Equals("Car_List") & n.ID == CarList.TypeID).FirstOrDefault().TypeName_CHS,
                        }
                    );
            }
            else
            {
                return (from CarList in db.Set<Car_List>()
                        join set in db.Set<Car_Set>() on CarList.fk_Set equals set.pk_Set
                        join User in db.Set<User_List>() on CarList.fk_User equals User.pk_User
                        join Department in db.Set<User_Department>() on CarList.fk_Department equals Department.pk_Department
                        where CarList.Title.Contains(Title)
                        select new
                        {

                            CarList.pk_Car,
                            CarList.Title,
                            CarList.CreateDate,
                            CarTitle = set.Title,
                            CarList.IsCheck,
                            CarList.CheckDate,
                            User.FullName,
                            CarList.IsBack,
                            CarList.BackDate,
                            Department.DepartmentName,
                            CarList.CreateUser,
                            CarList.StatusID,
                            CarList.CheckUser,
                            StatusName_CHS = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Car_Set") & n.ID == set.StatusID).FirstOrDefault().StatusName_CHS,
                            Visual = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Car_Set") & n.ID == set.StatusID).FirstOrDefault().Visual,
                            TypeName_CHS = db.Set<Sys_TypeID>().Where(n => n.TableName.Equals("Car_Set") & n.ID == set.TypeID).FirstOrDefault().TypeName_CHS,
                        }
                    );
            }
        }

        /// <summary>
        /// 添加，修改车辆管理
        /// </summary>
        /// <param name="s"></param>
        /// <param name="Type"></param>
        public void GetSet(Car_Set s, string Type)
        {
            if (Type.Equals("Add"))
            {
                //s.StatusID = 0;
                db.Set<Car_Set>().Add(s);
            }
            else if (Type.Equals("Edit"))
            {
                db.Set<Car_Set>().AddOrUpdate(s);
            }
        }

        /// <summary>
        /// 添加，修改车辆信息
        /// </summary>
        /// <param name="Card"></param>
        /// <param name="CarSet"></param>
        /// <param name="ViewCar"></param>
        public void AddCarListInfo(Car_List Card, string Type, int pk_User)
        {
            if (Type.Equals("Add"))
            {
                Card.StatusID = 0;
                Card.fk_User = pk_User;
                Card.CreateDate = DateTime.Now;
                Card.fk_Department = db.Set<User_List>().SingleOrDefault(n => n.pk_User == pk_User).fk_Department;
                Card.CreateUser = pk_User;
                Card.IsCheck = false;
                Card.IsBack = false;
                Card.IsCheck = false;
                Card.CheckUser = null;
                Card.CheckDate = null;
                Card.StatusID = 0;

                db.Set<Car_List>().Add(Card);
            }
            else if (Type.Equals("Edit"))
            {
                if (Card.CreateUser == pk_User)
                {
                    Card.IsBack = true;
                }
                Card.ModifyUser = pk_User;
                Card.ModifyDate = DateTime.Now;
                db.Set<Car_List>().AddOrUpdate(Card);
            }

        }

        /// <summary>
        /// 获取车辆管理类型
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetMyModels<T>(Expression<Func<T, bool>> whereLabmal) where T : class
        {
            return db.Set<T>().Where(whereLabmal);
        }

        /// <summary>
        /// 删除审核车辆
        /// </summary>
        /// <param name="l"></param>
        public void DeteleCheck(Car_List l)
        {
            db.Set<Car_List>().Remove(l);
        }

        /// <summary>
        /// 删除车辆
        /// </summary>
        /// <param name="s"></param>
        public void DeteleCarSet(Car_Set s)
        {
            db.Set<Car_Set>().Remove(s);

            List<Car_List> list = db.Set<Car_List>().Where(n => n.fk_Set == s.pk_Set).ToList();

            db.Set<Car_List>().RemoveRange(list);
        }

        /// <summary>
        /// pk_Car删除车辆审核记录
        /// </summary>
        /// <param name="s"></param>

        public void DelCarListData(int pk_Car)
        {
            Car_List data = db.Set<Car_List>().Where(n => n.pk_Car == pk_Car).FirstOrDefault();

            db.Set<Car_List>().Remove(data);
        }


        /// <summary>
        /// 删除用车管理
        /// </summary>
        /// <param name="cl"></param>
        public void DeteleCarInfo(Car_List cl)
        {
            db.Set<Car_List>().Remove(cl);
        }


        /// <summary>
        /// 获取接受人信息,带模糊查询
        /// </summary>
        /// <returns></returns>
        public IEnumerable<object> GetUserList(string SelName)
        {
            return (from list in db.Set<User_List>()
                    join Department in db.Set<User_Department>() on list.fk_Department equals Department.pk_Department
                    join Role in db.Set<Role_Name>() on list.RoleValue equals Role.pk_Name
                    where list.FullName.Contains(SelName)
                    select new
                    {
                        Department.DepartmentName,
                        list.Address1,
                        list.FullName,
                        list.UserName,
                        Role.RoleName_CHS,
                        list.Phone1
                    }
                );
        }

        /// <summary>
        /// 修改车辆审核
        /// </summary>
        /// <param name="c"></param>
        /// <param name="Type"></param>
        public void Update(Car_List c, int User)
        {
            // Car_List CList = db.Set<Car_List>().Where(n => n.pk_Car == c.pk_Car).FirstOrDefault();
            c.ModifyUser = User;
            c.ModifyDate = DateTime.Now;
           
            c.CheckUser = User;
            c.CheckDate = DateTime.Now;
            c.CheckDepartment = db.Set<User_List>().Where(n => n.pk_User == User).FirstOrDefault().fk_Department;
            //c.IsBack = true;
            //c.BackDate = DateTime.Now;
            if (c.StatusID == 1)
            {
                c.IsCheck = true;
                //修改车辆状态
                Car_Set CS = db.Set<Car_Set>().FirstOrDefault(n => n.pk_Set == c.fk_Set);
                CS.StatusID = 1;
                db.Set<Car_Set>().AddOrUpdate(CS);
            }
            else
            {
                c.IsCheck = false;
            }
            db.Set<Car_List>().AddOrUpdate(c);
        }
        //实现接口
        public bool SaveChanges()
        {
            return db.SaveChanges() > 0;
        }
    }
}


